package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ita.dao.IUserDao;
import com.ita.entity.User;
import com.ita.util.DbUtil;

public class UserDaoImpl implements IUserDao {

	@Override
	public int insertUser(User user) {
		String sql="insert into userPic values(?,?,?,?,?)";
		Connection conn=DbUtil.getConnection();
		int line=0;
		PreparedStatement ps;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserId());
			ps.setString(2, user.getUserName());
			ps.setString(3, user.getPassword());
			ps.setString(4, user.getAddress());
			ps.setString(5, user.getUserPic());
			line=ps.executeUpdate();
			DbUtil.free(conn, ps, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return line;
	}

	@Override
	public User login(String userName, String password) {
		String sql="select * from userPic where userName=? and password=?";
		Connection conn=DbUtil.getConnection();
		User user=new User();
		PreparedStatement ps;
		try {
			
			ps=conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				user.setUserId(rs.getString(1));
				user.setUserName(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setAddress(rs.getString(4));
				user.setUserPic(rs.getString(5));
				return user;
			}
			DbUtil.free(conn, ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public User queryById(String userId) {
		// TODO Auto-generated method stub
		String sql="select * from userPic where userId=?";
		Connection conn=DbUtil.getConnection();
		PreparedStatement ps;
		User user=new User();
		try {
			ps=conn.prepareCall(sql);
			ps.setString(1, userId);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				user.setUserId(rs.getString(1));
				user.setUserName(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setAddress(rs.getString(4));
				user.setUserPic(rs.getString(5));
				System.out.println("query:"+user.toString());
				return user;
			}
			DbUtil.free(conn, ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<User> queryAllUser() {
		// TODO Auto-generated method stub
		List<User> list=new ArrayList<User>();
		String sql="select * from userPic";
		Connection conn=DbUtil.getConnection();
		PreparedStatement ps;
		try {
			ps=conn.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				User user=new User();
				user.setUserId(rs.getString(1));
				user.setUserName(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setAddress(rs.getString(4));
				user.setUserPic(rs.getString(5));
				list.add(user);
				System.out.println(user.toString());
			}
			DbUtil.free(conn, ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int deleteById(String userId) {
		// TODO Auto-generated method stub
		String sql="delete from userPic where userId=?";
		Connection conn=DbUtil.getConnection();
		int line=0;
		PreparedStatement ps;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, userId);
			line=ps.executeUpdate();
			System.out.println("delete user success, the id of user is:"+userId);
			DbUtil.free(conn, ps, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return line;
	}

	@Override
	public int updateUser(User user) {
		String sql="update userPic set userName=?,password=?"
				+ ",address=?,userPic=? where userId=?";
		Connection conn=DbUtil.getConnection();
		int line=0;
		PreparedStatement ps;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getAddress());
			ps.setString(4, user.getUserPic());
			ps.setString(5, user.getUserId());
			line=ps.executeUpdate();
			DbUtil.free(conn, ps, null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return line;
	}

}
